package com.mall.db.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.db.mapper.UmsResourceMapper;
import com.mall.pojo.UmsResource;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class UmsResourceApilmpl implements UmsResourceApi {

    @Autowired
    private UmsResourceMapper umsResourceMapper;

    //分页模糊查询后台资源
    @Override
    public Page<UmsResource> list(Integer pageNum, Integer pageSize, Long categoryId, String nameKeyword, String urlKeyword) {
        Page<UmsResource> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<UmsResource> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(categoryId != null, UmsResource::getCategoryId, categoryId);
        wrapper.like(nameKeyword != null, UmsResource::getName, nameKeyword);
        wrapper.like(urlKeyword != null, UmsResource::getUrl, urlKeyword);
        return umsResourceMapper.selectPage(page, wrapper);
    }

    //查询所有后台资源
    @Override
    public List<UmsResource> listAll() {
        LambdaQueryWrapper<UmsResource> wrapper = new LambdaQueryWrapper<>();
        return umsResourceMapper.selectList(wrapper);
    }

    //添加后台资源
    @Override
    public void create(UmsResource umsResource) {
        umsResourceMapper.insert(umsResource);
    }

    //根据ID删除后台资源
    @Override
    public void delete(Long id) {
        umsResourceMapper.deleteById(id);
    }

    //修改后台资源
    @Override
    public void update(Long id, UmsResource umsResource) {
        LambdaQueryWrapper<UmsResource> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UmsResource::getId, id);
        umsResourceMapper.update(umsResource, wrapper);
    }

    //根据ID获取资源详情
    @Override
    public UmsResource selectById(Long id) {
        return umsResourceMapper.selectById(id);
    }

    @Override
    public List<UmsResource> findByIds(List<Long> resourceIds) {
        return umsResourceMapper.selectBatchIds(resourceIds);
    }
}
